// define the namespace
syslib.namespace("syslib.ui");

// tag conversor
syslib.ui.TextEditorTag = __class(syslib.ui.TagConversor, {
	component : null, 
	
	execute : function(){
		// get the tag attributes
		var attributes = this.__attributes;
	
		// create the options
		var options = {};
		
		// parse the attributes
		if(__valid(attributes.maxlength)) options["maxLength"] = attributes.laxlength;
		if(__valid(attributes.style)) options["style"] = attributes.style;
		if(__valid(attributes.id)) options["id"] = attributes.id;
		if(__valid(attributes.linebreak) && attributes.linebreak === false || attributes.linebreak === "false"){
			options["lineBreak"] = false;
		}
		
		// create the texteditor
		var textEditor = new syslib.ui.TextEditor(options);
		
		// append
		textEditor.appendTo(this.__container);
		
		// store
		this.component = textEditor;
	}, 
	
	getComponent : function(){
		return this.component;
	}
});

// component definition
syslib.ui.TextEditor = __class(syslib.ui.VisualTag, {
	__componentName : "syslib.ui.TextEditor",
	__options : {},
	references : null, 
	
	__construct : function(options){
		// set the options
		this.__options = __extend({
			id : syslib.element.generateID(), 
			lineBreak : true,
			maxLength : false, 
			style : "", 
			content : ""
		});
		
		// load the style
		syslib.loadStyleForComponent('texteditor');
	},

	__createElement : function(){
		// create the JSON structure
		var elemtJSON = ["table", {"border" : 0, "cellpadding" : 0, "cellspacing" : 0, "style" : this.__options.style},
			["tbody",
				["tr",
					["td", {"innerName" : "textareatd", "class" : "syslib-texteditor-textarea-container"},
						["textarea", {"class" : "syslib-texteditor-textarea", "innerName" : "textarea", "id" : this.__options.id},
							this.__options.content
						]
					]
				],
				["tr",
					["td", {"innerName" : "bottombartd", "class" : "syslib-texteditor-bottombar"},
						"&nbsp;"
					]
				]
			]
		];

		// create the object
		var textEditor 	= syslib.element.createFromJSON(false, elemtJSON);
		
		// store the element and the references
		this.__element	= textEditor.element;
		this.references	= textEditor.references;
	},

	finalize : function(){
		// get the attributes
		// var attribs = this.__attributes;
		
		// get the options and references
		var references 	= this.references;
		var options		= this.options;

		// count the char number
		syslib.ui.TextEditor.countChars(references.textarea, references.bottombartd);
		
		// keydown event
		syslib.event.listen('keydown', references.textarea, function(evt){
			// get the keycode
			var code = syslib.event.getKeyCode(evt);
			
			// check if the enter has been pressed
			if(code == 13){
				// verify if line break is allowed
				if(this.__options.lineBreak === false || this.__options.lineBreak === "false") {
					// stop the event propagation
					syslib.event.stopPropagation(evt);
				}
			}
			
			// check the maxlength
			if(options.maxlength){
				if(syslib.event.isSpecialCode(evt)){
					return true;
				} else {
					if(syslib.ui.TextEditor.countChars(references.textarea, references.bottombartd) >= options.maxlength ){
						syslib.event.stopPropagation(evt);
					}
				}
			}
		}.applyTo(this));

		// keyup event
		syslib.event.listen('keyup', references.textarea, function(evt){
			syslib.ui.TextEditor.countChars(references.textarea, references.bottombartd);
		}.applyTo(this));
	}
});

syslib.ui.TextEditor.countChars = function(textarea, updateElement){
	var count = textarea.value.length;
	syslib.element.update(updateElement, count);

	return count;
};